package org.biouno.unochoice;

import com.alibaba.fastjson.JSONObject;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.util.ListBoxModel;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import java.io.Serializable;
import java.util.regex.Pattern;

import static org.biouno.unochoice.util.Utils.getNodes;

@ExportedBean
public class DebList extends AbstractDescribableImpl<DebList> implements Serializable {

    private String name;
    private String version;


    @DataBoundConstructor
    public DebList(String name, String version) {
        this.name = name;
        this.version = version;
    }


    @Exported
    @Whitelisted
    public String getName() {
        return name;
    }

    @Exported
    @Whitelisted
    public String getVersion() {
        return version;
    }


    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        return jsonObject.toString();
    }


    public JSONObject toJSONObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("version", version);
        return jsonObject;
    }

    private final static Pattern SCP_LIKE = Pattern.compile("(.*):(.*)");


    @Extension
    public static class DescriptorImpl extends Descriptor<DebList> {

        public ListBoxModel doFillNameItems(@AncestorInPath Item project) {

            ListBoxModel listBoxModel = new ListBoxModel();
            getNodes().forEach(node -> {
                listBoxModel.add(node);
            });
            return listBoxModel;
        }

        public ListBoxModel doFillVersionItems(@AncestorInPath Item project, @QueryParameter String name) {

            ListBoxModel listBoxModel = new ListBoxModel();
            getNodes().forEach(node -> {
                listBoxModel.add(node);
            });
            return listBoxModel;
        }

        @Override
        public String getDisplayName() {
            return "";
        }
    }
}
